<template>
  <div class="user-register">
    <div class="register-container">
      <div class="register-box">
        <!-- 左侧装饰图 -->
        <div class="register-left">
          <img src="https://picsum.photos/600/800?random=11" alt="background" class="bg-image">
          <div class="overlay">
            <h2>欢迎加入</h2>
            <p>智慧旅游服务平台，让旅行更美好</p>
          </div>
        </div>
        
        <!-- 右侧注册表单 -->
        <div class="register-right">
          <div class="register-form-container">
            <h3>用户注册</h3>
            <p class="register-desc">请填写以下信息完成注册</p>
            
            <el-form 
              :model="registerForm" 
              :rules="rules" 
              ref="registerForm" 
              class="register-form">
              <el-form-item prop="username">
                <el-input 
                  v-model="registerForm.username" 
                  prefix-icon="el-icon-user"
                  placeholder="请输入用户名">
                </el-input>
              </el-form-item>
              
              <el-form-item prop="password">
                <el-input 
                  type="password" 
                  v-model="registerForm.password"
                  prefix-icon="el-icon-lock"
                  placeholder="请输入密码">
                </el-input>
              </el-form-item>
              
              <el-form-item prop="confirmPassword">
                <el-input 
                  type="password" 
                  v-model="registerForm.confirmPassword"
                  prefix-icon="el-icon-lock"
                  placeholder="请确认密码">
                </el-input>
              </el-form-item>
              
              <el-form-item prop="email">
                <el-input 
                  v-model="registerForm.email"
                  prefix-icon="el-icon-message"
                  placeholder="请输入邮箱">
                </el-input>
              </el-form-item>
              
              <el-form-item prop="phone">
                <el-input 
                  v-model="registerForm.phone"
                  prefix-icon="el-icon-mobile-phone"
                  placeholder="请输入手机号">
                </el-input>
              </el-form-item>
              
              <el-button 
                type="primary" 
                @click="handleRegister" 
                class="register-button">
                注册
              </el-button>
            </el-form>
            
            <div class="register-footer">
              <p>已有账号？ <el-button type="text" @click="goToLogin" class="login-btn">立即登录</el-button></p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'UserRegister',
  data() {
    // 密码验证规则
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.registerForm.confirmPassword !== '') {
          this.$refs.registerForm.validateField('confirmPassword')
        }
        callback()
      }
    }
    // 确认密码验证规则
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        email: '',
        phone: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatePass, trigger: 'blur' },
          { min: 6, message: '密码长度至少为6个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async handleRegister() {
      try {
        await this.$refs.registerForm.validate()
        const { confirmPassword, ...registerData } = this.registerForm
        await request.post('/api/user/register', registerData)
        this.$message.success('注册成功')
        this.$router.push('/login')
      } catch (error) {
        console.error('注册失败:', error)
        this.$message.error(error.response?.data?.message || '注册失败')
      }
    },
    goToLogin() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.user-register {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
}

.register-container {
  width: 1000px;
  height: 700px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.register-box {
  display: flex;
  height: 100%;
}

.register-left {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.bg-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom, rgba(0,0,0,0.3), rgba(0,0,0,0.7));
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  padding: 40px;
  text-align: center;
}

.overlay h2 {
  font-size: 32px;
  margin-bottom: 20px;
  font-weight: 500;
}

.overlay p {
  font-size: 16px;
  opacity: 0.9;
}

.register-right {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.register-form-container {
  width: 80%;
  margin: 0 auto;
}

.register-form-container h3 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 10px;
  font-weight: 500;
}

.register-desc {
  color: #909399;
  margin-bottom: 30px;
}

.register-form {
  margin-top: 20px;
}

.register-form :deep(.el-input__inner) {
  height: 45px;
  line-height: 45px;
}

.register-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  letter-spacing: 2px;
  margin-top: 20px;
}

.register-footer {
  margin-top: 20px;
  text-align: center;
  color: #909399;
}

.login-btn {
  color: #409EFF;
  padding: 0;
}

.login-btn:hover {
  color: #66b1ff;
}

/* 响应式设计 */
@media screen and (max-width: 1024px) {
  .register-container {
    width: 90%;
    height: auto;
    min-height: 700px;
  }
}

@media screen and (max-width: 768px) {
  .register-box {
    flex-direction: column;
  }
  
  .register-left {
    display: none;
  }
  
  .register-right {
    padding: 30px 20px;
  }
  
  .register-form-container {
    width: 100%;
  }
}
</style> 